/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.weijiwufang.ui.action;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import org.dusin.weijiwufang.bo.VisitorBean;
import org.dusin.weijiwufang.service.VisitorService;
import org.dusin.wufang.util.WuFangUtil;

/**
 *
 * @author dusin
 */
public class UserManageAction implements ActionListener {

    private static final String title = "用户管理";
    private JFrame mainFrame;  //当前面板
    private JTabbedPane pane;
    VisitorService visitorService;
    JTable userTable;

    public UserManageAction(JFrame mainFrame,JTabbedPane tabbedPane, VisitorService visitorService) {
        this.mainFrame = mainFrame;
        this.pane = tabbedPane;
        this.visitorService = visitorService;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        int i = pane.indexOfTab(title);
        if (i == -1) {
            pane.addTab(title, null, makePanel(),
                    title);
            i = pane.getTabCount() - 1;
            pane.setTabComponentAt(i,
                    new ButtonTabComponent(pane, "/org/dusin/weijiwufang/image/group.png"));
        }
        pane.setSelectedIndex(i);

    }

    protected JComponent makePanel() {
        JPanel panel = new JPanel(false);
        panel.setLayout(new BorderLayout());

        JToolBar toolBar = new JToolBar("Still draggable");

        JButton addButton = new JButton("增加", WuFangUtil.createImageIcon("/org/dusin/weijiwufang/image/user_add.png"));
        addButton.addActionListener(this.new AddVisitorAction());

        JButton updateButton = new JButton("修改", WuFangUtil.createImageIcon("/org/dusin/weijiwufang/image/user_edit.png"));
        updateButton.addActionListener(this.new UpdateVisitorAction());

        JButton delButton = new JButton("删除", WuFangUtil.createImageIcon("/org/dusin/weijiwufang/image/user_delete.png"));
        delButton.addActionListener(this.new DelVisitorAction());


        toolBar.add(addButton);
        toolBar.add(updateButton);
        toolBar.add(delButton);
        toolBar.add(new JSeparator(SwingConstants.VERTICAL));

        //addButtons(toolBar);
        panel.add(toolBar, BorderLayout.NORTH);

        JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        userTable = new javax.swing.JTable();
        userTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null},
                    {null, null, null, null, null, null, null, null, null}
                },
                new String[]{
                    "id", "用户名", "密码", "工号", "姓名", "用户权限", "联系方式", "备注", "创建时间"
                }) {
            Class[] types = new Class[]{
                java.lang.Long.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.sql.Timestamp.class
            };
            boolean[] canEdit = new boolean[]{
                false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        userTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(userTable);
        panel.add(jScrollPane1, BorderLayout.CENTER);
        loadData();        
        return panel;
    }

    public void loadData() {
        DefaultTableModel tableModel = (DefaultTableModel) userTable.getModel();
        tableModel.setRowCount(0);
        List<VisitorBean> list = null;
        try {
            list = visitorService.findAll();
            for (VisitorBean v : list) {
                tableModel.addRow(new Object[]{v.getId(), v.getLoginName(), v.getPassword(), v.getEmployeeNo(), v.getRealName(), VisitorBean.getLevelName(v.getLevel()), v.getContact(), v.getNote(), v.getCreateTime()});
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    class AddVisitorAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            UserInfoDialog userInfoDialog = new UserInfoDialog(mainFrame, true);
            //userInfoDialog.set
            //mainFrame.set
            userInfoDialog.setLocationRelativeTo(mainFrame);
            userInfoDialog.setTitle("增加用户信息");
            userInfoDialog.setVisible(true);
            VisitorBean user;
            user = userInfoDialog.getUser();
            if (user != null) {
                visitorService.addVisitor(user);
                loadData();
            }
            //userInfoDialog.setVisible(false);
            userInfoDialog.dispose();

        }
    }

    class UpdateVisitorAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {

            VisitorBean user = new VisitorBean();
            UserInfoDialog userInfoDialog = new UserInfoDialog(mainFrame, true);
            userInfoDialog.setLocationRelativeTo(mainFrame);
            userInfoDialog.setTitle("修改用户信息");
            int selectedRowIndex = userTable.getSelectedRow();
            if (selectedRowIndex != -1) {

                DefaultTableModel tableModel = (DefaultTableModel) userTable.getModel();
                long currentId = (Long) (tableModel.getValueAt(selectedRowIndex, 0));                
                
                String logName = (String) (tableModel.getValueAt(selectedRowIndex, 1));
                user.setLoginName(logName);

                String pass = (String) (tableModel.getValueAt(selectedRowIndex, 2));
                user.setPassword(pass);

                String employNo = (String) (tableModel.getValueAt(selectedRowIndex, 3));
                user.setEmployeeNo(employNo);

                String realName = (String) (tableModel.getValueAt(selectedRowIndex, 4));
                user.setRealName(realName);

                String levelName = (String) (tableModel.getValueAt(selectedRowIndex, 5));
                user.setLevel(VisitorBean.getLevelCode(levelName));

                String contact = (String) (tableModel.getValueAt(selectedRowIndex, 6));
                user.setContact(contact);

                String note = (String) (tableModel.getValueAt(selectedRowIndex, 7));
                user.setNote(note);
                userInfoDialog.setUser(user);

                userInfoDialog.setVisible(true);

                user = userInfoDialog.getUser();
                user.setId(currentId);
                if (user != null) {
                    visitorService.updateVisitor(user);
                }

                userInfoDialog.dispose();
                loadData();
            } else {
                JOptionPane.showMessageDialog(null, "请选择一项");
            }

        }
    }

    class DelVisitorAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            int selectedRowIndex = userTable.getSelectedRow();
            if (selectedRowIndex != -1) {
                if (JOptionPane.showConfirmDialog(null, "你确定删除吗？") == JOptionPane.OK_OPTION) {
                    DefaultTableModel tableModel = (DefaultTableModel) userTable.getModel();
                    long currentId = (Long) (tableModel.getValueAt(selectedRowIndex, 0));                    
                    visitorService.deleteVisitor(currentId);
                    loadData();
                }

            } else {
                JOptionPane.showMessageDialog(null, "请选择一项删除");
            }

        }
    }
}
